package com.bsierp.util.tags;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;


import com.bsierp.business.pojo.User;
import com.bsierp.util.Constants;
import com.bsierp.util.hibernate.HibernateSessionFactory;

public class DisplayContactPersonHtmlSelect extends TagSupport {
	
	 String id;
		public String getId() {
			  return id;
			 }
			 public void setId(String id) {
			  this.id = id;
			 }
	
private Log log = LogFactory.getLog(this.getClass().getName());
 
public int doEndTag( ) throws JspException
{
	
  JspWriter out = pageContext.getOut();
  
  String hql="select cp.id.user_1.userId,cp.id.user_1.userName  from ContactPerson cp where cp.id.user=:user";

try{
	HttpSession ses = pageContext.getSession();
	boolean isedit=false;

	User u=null;
	if(ses.getAttribute(Constants.userLogin_sessionindex)!=null){
		u=(User)ses.getAttribute(Constants.userLogin_sessionindex);
	}
	
	
	SessionFactory sessionFactory= HibernateSessionFactory.getSessionFactory();
	Session session=sessionFactory.openSession();
	Transaction tx=null;
	String option_str="";
		try {
		tx=session.beginTransaction();
		Query q=session.createQuery(hql);
		q.setEntity("user", u);
		List l=q.list();
		
		for(int i=0;i<l.size();i++){
			Object[] pc=(Object[])l.get(i);	
			option_str=option_str+"<option value=\""+pc[0]+"\">"+pc[1]+"</option>"   ;
			}
		}catch(Exception e){
			e.printStackTrace();
 		}finally{
			session.close();
		}
		
		
  out.println("<select name=\"select\" id=\""+this.getId()+"\">" );  
  out.println("<option value=\"0\" selected=\"selected\">不限 </option>");
  out.println(option_str);
  out.println("</select>");
	
}catch (Exception ex)
  {
    throw new JspTagException("IOException:" + ex.toString());
  }
  return super.doEndTag();
}
}

